﻿using JetBrains.Metadata.Reader.API;
using JetBrains.ReSharper.Daemon.Stages;
using JetBrains.ReSharper.Daemon.Stages.Dispatcher;
using JetBrains.ReSharper.Psi;
using JetBrains.ReSharper.Psi.Modules;
using JetBrains.ReSharper.Psi.Tree;

namespace MugenMvvmToolkit.ReSharper.Infrastructure
{
    public abstract class ElementProblemAnalyzerBase<T> : IElementProblemAnalyzer where T : ITreeNode
    {
        #region Properties

        /// <summary>
        ///     Gets the TypeProvider.
        /// </summary>
        protected TypeProvider TypeProvider { get; private set; }

        /// <summary>
        ///     Gets the MethodProvider.
        /// </summary>
        protected MethodProvider MethodProvider { get; private set; }

        /// <summary>
        ///     Gets the EventProvider.
        /// </summary>
        protected EventProvider EventProvider { get; private set; }

        #endregion

        #region Methods

        protected abstract void Run(T element, ElementProblemAnalyzerData data,
            IHighlightingConsumer consumer);

        private void Initialize(IPsiModule module, IModuleReferenceResolveContext context)
        {
            TypeProvider = TypeProvider.Create(module, context);
            MethodProvider = MethodProvider.Create(TypeProvider);
            EventProvider = EventProvider.Create(TypeProvider);
        }

        #endregion

        #region Implementation of IElementProblemAnalyzer

        void IElementProblemAnalyzer.Run(ITreeNode element, ElementProblemAnalyzerData data,
            IHighlightingConsumer consumer)
        {
            Initialize(data.Process.PsiModule, data.Process.SourceFile.GetResolveContext());
            Run((T) element, data, consumer);
        }

        #endregion
    }
}